package com.android.camtube;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class CamTube extends Activity {
	
/**************************/
/*****   Attributes   *****/
/**************************/
	
	CameraPreview camPreview;
	CameraHandler camHandler;
	Button btn_shot;
    Button btn_rec;
    Button btn_stop;
	
	
	
/*************************/
/*****   Listeners   *****/
/*************************/

	// Picture button listener
	private View.OnClickListener btnShotListener = new View.OnClickListener() {
        public void onClick(View v) {
        	camHandler.takePicture();
        }

    };
    
    // Recording button listener
    private View.OnClickListener btnRecListener = new View.OnClickListener() {
        public void onClick(View v) {
        	File file =  new File(String.format("/sdcard/DCIM/Camera/%d.mpg", System.currentTimeMillis()));
        	try {
				camHandler.startRecording(file.getAbsolutePath(), camPreview.getHolder().getSurface());
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
          
        }

    };
    
    // Stop recording
    private View.OnClickListener btnStopListener = new View.OnClickListener() {
        public void onClick(View v) {
        	try {
				camHandler.stopRecording();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
          
        }

    };
    
    
    
/***********************/
/*****   Methods   *****/
/***********************/
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //
        setContentView(R.layout.main);
        
        //
        //FrameLayout frameLayout = (FrameLayout) findViewById(R.id.layout_main);
        //layout.addView(camPreview);
        
        // Create our Preview view and set it as the content of our activity.    
        
        //camPreview = new CameraPreview(this, camHandler);
        camPreview = (CameraPreview) findViewById(R.id.preview_surface);
        camHandler = new CameraHandler();
        camPreview.initCamPreview(camHandler);
        
        // Set click listeners
        btn_shot = (Button) findViewById(R.id.btn_shot);
        btn_rec  = (Button) findViewById(R.id.btn_rec);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_shot.setOnClickListener(btnShotListener);
        btn_rec.setOnClickListener(btnRecListener);
        btn_stop.setOnClickListener(btnStopListener);
        
    }
    
    @Override
    public void onDestroy() {
    	camHandler = null;
    	camPreview = null;
    	super.onDestroy();
    }
    
    
    
}